Eclipse Platform
Pre-release 3.0

org.eclipse.jface.text.formatter
Class ContentFormatter2

java.lang.Object
  extended byorg.eclipse.jface.text.formatter.ContentFormatter2
All Implemented Interfaces:
IContentFormatter, IContentFormatterExtension, IContentFormatterExtension2

public class ContentFormatter2
extends Object
implements IContentFormatter, IContentFormatterExtension, IContentFormatterExtension2

Improved standard implementation of IContentFormatter. The formatter supports three operation modi: partition aware, partition unaware and context based.

In the partition aware mode, the formatter determines the partitioning of the document region to be formatted. For each partition it determines all document positions which are affected when text changes are applied to the partition. Those which overlap with the partition are remembered as character positions. These character positions are passed over to the formatting strategy registered for the partition's content type. The formatting strategy returns a string containing the formatted document partition as well as the adapted character positions. The formatted partition replaces the old content of the partition. The remembered document postions are updated with the adapted character positions. In addition, all other document positions are accordingly adapted to the formatting changes.

In the partition unaware mode, the document's partitioning is ignored and the document is considered consisting of only one partition of the content type IDocument.DEFAULT_CONTENT_TYPE. The formatting process is similar to the partition aware mode, with the exception of having only one partition.

The context based mode is supported by the extension interface IContentFormatterExtension2 and supersedes the previous modes. Clients using context based formatting call the method format(IDocument, IFormattingContext) with a properly initialized formatting context.
The formatting context must be set up according to the desired formatting mode:

Depending on the registered formatting strategies, more context information must be passed in the formatting context, like e.g. CONTEXT_PREFERENCES.

Note that in context based mode the content formatter is fully reentrant, but not thread-safe. Formatting strategies are therefore allowed to recursively call the method format(IDocument, IFormattingContext). The formatting context is saved between calls to this method.

Usually, clients instantiate this class and configure it before using it.

See Also:
IContentFormatter, IContentFormatterExtension2, IDocument, ITypedRegion, Position

Constructor Summary
ContentFormatter2()
          Creates a new content formatter.
 
Method Summary
 void enablePartitionAwareFormatting(boolean enable)
          Sets whether the formatter operates in partition aware mode.
 void format(IDocument document, IFormattingContext context)
          Formats the given region of the specified document.
 void format(IDocument document, IRegion region)
          Formats the given region of the specified document.The formatter may safely assume that it is the only subject that modifies the document at this point in time.
 String getDocumentPartitioning()
          Returns the partitioning this content formatter is using.
 IFormattingStrategy getFormattingStrategy(String contentType)
          Returns the formatting strategy registered for the given content type.
protected  boolean positionAboutToBeAdded(IDocument document, String category, Position position)
          The given position is about to be added to the given position category of the given document.
 void setDocumentPartitioning(String partitioning)
          Sets the document partitioning to be used by this formatter.
 void setFormattingStrategy(IFormattingStrategy strategy)
          Registers the master strategy for this content formatter.
 void setFormattingStrategy(IFormattingStrategy strategy, String type)
          Registers a slave strategy for a particular content type.
 void setPartitionManagingPositionCategories(String[] categories)
          Deprecated. incompatible with an open set of document partitionings. The provided information is only used if this formatter can not compute the partition managing position categories.
protected  void updateAffectedPositions(IDocument document, int[] positions, int offset)
          Updates all the overlapping positions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ContentFormatter2

public ContentFormatter2()
Creates a new content formatter.

The content formatter operates by default in the partition-aware mode. There are no preconfigured formatting strategies. It will use the default document partitioning if not further configured. The context based mode is enabled by calls to format(IDocument, IFormattingContext.

Method Detail

setPartitionManagingPositionCategories

public void setPartitionManagingPositionCategories(String[] categories)
Deprecated. incompatible with an open set of document partitionings. The provided information is only used if this formatter can not compute the partition managing position categories.

Informs this content formatter about the names of those position categories which are used to manage the document's partitioning information and thus should be ignored when this formatter updates positions.

Parameters:
categories - the categories to be ignored

setDocumentPartitioning

public void setDocumentPartitioning(String partitioning)
Sets the document partitioning to be used by this formatter.

Parameters:
partitioning - the document partitioning
Since:
3.0

getDocumentPartitioning

public String getDocumentPartitioning()
Description copied from interface: IContentFormatterExtension
Returns the partitioning this content formatter is using.

Specified by:
getDocumentPartitioning in interface IContentFormatterExtension
Returns:
the partitioning this content formatter is using

enablePartitionAwareFormatting

public void enablePartitionAwareFormatting(boolean enable)
Sets whether the formatter operates in partition aware mode.

Parameters:
enable - true iff partition aware mode should be enabled, false otherwise.

getFormattingStrategy

public IFormattingStrategy getFormattingStrategy(String contentType)
Description copied from interface: IContentFormatter
Returns the formatting strategy registered for the given content type.

Specified by:
getFormattingStrategy in interface IContentFormatter
Parameters:
contentType - the content type for which to look up the formatting strategy
Returns:
the formatting strategy for the given content type, or null if there is no such strategy

format

public void format(IDocument document,
                   IRegion region)
Description copied from interface: IContentFormatter
Formats the given region of the specified document.The formatter may safely assume that it is the only subject that modifies the document at this point in time.

Specified by:
format in interface IContentFormatter
Parameters:
document - the document to be formatted
region - the region within the document to be formatted

format

public void format(IDocument document,
                   IFormattingContext context)
Description copied from interface: IContentFormatterExtension2
Formats the given region of the specified document.

The formatter may safely assume that it is the only subject that modifies the document at this point in time. This method is fully reentrant. Formatting strategies registered with IContentFormatterExtension2 objects are therefore allowed to recursively call this method on subregions of the region to be formatted.

The formatting process performed by format(IDocument, IFormattingContext) happens as follows:


setFormattingStrategy

public void setFormattingStrategy(IFormattingStrategy strategy,
                                  String type)
Registers a slave strategy for a particular content type.

If there is already a slave strategy registered for this type, the new strategy is registered instead of the old one. The content type type must be a valid content type of the registered partitioning of the formatter.

Note that slave strategies can only be registered if a master strategy has been registered before.

Parameters:
strategy - The formatting strategy to register as a slave strategy, or null to remove an existing one
type - The content type under which to register the slave strategy

setFormattingStrategy

public void setFormattingStrategy(IFormattingStrategy strategy)
Registers the master strategy for this content formatter. If there is already a master strategy registered, the new strategy is registered instead of the old one.

Note that slave strategies can only be registered if a master strategy has been registered before.

Parameters:
strategy - The formatting strategy to register as the master strategy, or null to remove the existing one

updateAffectedPositions

protected void updateAffectedPositions(IDocument document,
                                       int[] positions,
                                       int offset)
Updates all the overlapping positions. Note, all other positions are automatically updated by their document position updaters.

Parameters:
document - the document to has been formatted
positions - the adapted character positions to be used to update the document positions
offset - the offset of the document region that has been formatted

positionAboutToBeAdded

protected boolean positionAboutToBeAdded(IDocument document,
                                         String category,
                                         Position position)
The given position is about to be added to the given position category of the given document.

This default implementation enacts the same rule as the TextViewer, i.e. if the position is used for managing slave documents it is ensured that the slave document starts at a line offset.

Parameters:
document - the document
category - the position categroy
position - the position that will be added
Returns:
true if the position can be added, false if it should be ignored

Eclipse Platform
Pre-release 3.0

Copyright (c) IBM Corp. and others 2000, 2003. All Rights Reserved.